package cn.edu.scau.cmi.chenruilin.hibernate;

import java.util.List;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class TeacherDao {
	private static final Logger log = LoggerFactory.getLogger(TeacherDao.class);
	// property constants
	public static final String NAME = "name";
	private Session session;
	
	public Session getSession() {
		session = ScauCmiHibernateSessionFactoryUtil.getSession();
		return session;
	}
	
	public void save(Teacher instance) {
		log.debug("saving Teacher instance");
		try {
			getSession();
			Transaction transaction = session.beginTransaction();
			session.save(instance);
			transaction.commit();
			log.debug("save successful");
		} 
		catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public void delete(Teacher instance) {
		log.debug("deleting Teacher instance");
		try {
			getSession();
			Transaction transaction = session.beginTransaction();
			session.delete(instance);
			transaction.commit();
			log.debug("delete successful");
		}
		catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public void update(Teacher instance) {
		log.debug("updating dirty Teacher");
		try {
			getSession();
			Transaction transaction = session.beginTransaction();
			session.update(instance);
			transaction.commit();
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}
	
	public List findAll() {
		log.debug("finding all teacher");
		try {
			String queryStr = "from Teacher";
			Query queryObject = getSession().createQuery(queryStr);
			log.debug("find by property successful");
			return queryObject.list();
		}
		catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
		
	}
	
	
	/*
	public Teacher findById(Long id) {
		log.debug("finding Teacher instance with id: " + id);
		try {
			Teacher result = (Teacher) getSession().get("cn.edu.scau.cmi.chenruilin.orm.Teacher", id);
			log.debug("find by id successful");
			return result;
		}
		catch (RuntimeException re) {
			log.error("find by id failed", re);
			throw re;
		}
	}
	
	public List findByExample(Teacher instance) {
		log.debug("finding Teacher instance with example");
		try {
			List results = getSession().createCriteria("cn.edu.scau.cmi.chenruilin.orm.Teacher")
					.add(Example.create(instance)).list();
			log.debug("find by example successful");
			return results;
		}
		catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Teacher instance with property: " + propertyName + ", value: " + value);
		try {
			String queryStr = "from Teacher as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryStr);
			queryObject.setParameter(0, value);
			log.debug("find by property successful");
			return queryObject.list();
		}
		catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
		
	}
	
	public List findByName(String name) {
		return findByProperty("name", name);
	}
	
	
	public Teacher merge(Teacher instance) {
		log.debug("merging teacher");
		try {
			Teacher result = (Teacher) getSession().merge(instance);
			log.debug("merge successful");
			return result;
		}
		catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
		
	}
	
	public void attachDirty(Teacher instance) {
		log.debug("attaching dirty Teacher");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Teacher instance) {
		log.debug("attaching clean Teacher");
		try {
			getSession().buildLockRequest(LockOptions.NONE).lock(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	*/
	
}
